package com.example.iceman.stepcounterapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


import butterknife.Bind;
import butterknife.ButterKnife;

public class StepCounterActivity extends FragmentActivity implements View.OnClickListener, StepChangeListener {
    @Bind(R.id.start)
    Button start;
    @Bind(R.id.textView)
    TextView stepText;
    @Bind(R.id.log)
    Button log;
    @Bind(R.id.clear)
    Button clear;
    @Bind(R.id.orientation)
    TextView orientationText;
    @Bind(R.id.direction_img)
    ImageView directionImg;
    //    private StepCounter stepCounter;
    private NewStepCounter stepCounter;
    private boolean isStart = false;
    private int step = 0;
    private LogFragment fragment;
    /**
     * 只找到一张歪的指南针图。。。
     */
    private int imgOrientationOffset = -45;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.walk_listener_layout);
        ButterKnife.bind(this);
        start.setOnClickListener(this);
        log.setOnClickListener(this);
        clear.setOnClickListener(this);
        stepCounter = new NewStepCounter(this);
        stepCounter.setOnStepListener(this);
        directionImg.setRotation(imgOrientationOffset);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clear:
                clearlog();
                stepText.setText("0");
                orientationText.setText("");
                step = 0;
                break;
            case R.id.log:
//                showLogFragment();

                startActivity(new Intent(StepCounterActivity.this, StepCounterMainActivity.class));
                break;
            case R.id.start:
                startOrStopCount();
                break;
        }

    }

    private void showLogFragment() {
        if (fragment == null) {
            fragment = new LogFragment();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(Window.ID_ANDROID_CONTENT, fragment, "log");
            transaction.commit();
        } else {
            fragment.getFragmentManager().beginTransaction().show(fragment).commit();
        }
    }

    private void startOrStopCount() {
        isStart = !isStart;
        start.setText(isStart ? "停止" : "开始");
        if (isStart) {
            clearlog();
            stepCounter.start();
        } else {
            stepCounter.stop();
            directionImg.setRotation(imgOrientationOffset);
            step = 0;
        }
    }

    @Override
    public void onStep(int orientation) {
        step++;
        stepText.setText(String.valueOf(step));
        orientationText.setText(StepCounter.getOrientationString(orientation));
        directionImg.setRotation(orientation + imgOrientationOffset);

    }

    @Override
    public void onLog(String log) {
        if (fragment != null) {
            fragment.log(log);
        }
    }

    private void clearlog() {
        if (fragment != null) {
            fragment.clear();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (stepCounter != null) {
            stepCounter.stop();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            if (fragment != null && fragment.isVisible()) {
                fragment.getFragmentManager().beginTransaction().hide(fragment).commit();
                return true;
            } else {
                finish();
                return true;
            }
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
}

